﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class JsonTreeItemCustomAttribute
    {
        public string image { get; set; }
        public string color { get; set; }
    }

    public class JsonTreeItem
    {
        public string id { get; set; }
        public string text { get; set; }
        public string type { get; set; }
        public string iconCls { get; set; }
        public string state { get; set; }
        public string hasCheckbox { get; set; }
        public JsonTreeItemCustomAttribute attributes { get; set; }
        public IList<JsonTreeItem> children { get; set; }

        public string value { get; set; }
        public bool hasLogic { get; set; }
    }

    public class EasyUIController : Controller
    {
        //
        // GET: /EasyUI/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Datepicker()
        {
            return View();
        }

        public ActionResult TreeGrid()
        {
            return View();
        }

        public ActionResult GetTree(JsonTreeItem model)
        {
            JsonResult json = new JsonResult();
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            json.Data = GetChildren(model.id);
            return json;
        }


        private IList<JsonTreeItem> GetChildren(string id)
        {
            id = string.IsNullOrEmpty(id) ? "" : id + "_";
            IList<JsonTreeItem> items = new List<JsonTreeItem>();
            for (int i = 0; i < 5; i++)
            {
                string pro = id + (i + 1);
                JsonTreeItem node = new JsonTreeItem();
                node.id = pro;
                node.text = "text" + pro;
                node.type = "type" + pro;                
                node.children = new List<JsonTreeItem>();
                node.attributes = new JsonTreeItemCustomAttribute();
                if ((i % 2) == 0)
                {
                    node.hasCheckbox = "false";
                    node.state = "closed";                    
                }
                else
                {                    
                    node.state = "open";
                    //node.attributes.color = "#0000ff";
                    node.attributes.image = "../../Content/Image/个人.png";
                }
                node.value = "11";
                node.hasLogic = true;
                items.Add(node);
            }
            return items;
        }


        private JsonTreeItem GetNode(int index,bool hasLogic = false,string icon = "")
        {
            JsonTreeItem node = new JsonTreeItem();
            node.id = "id" + index;
            node.text = "text" + index;
            node.type = "type" + index;
            node.state = "closed";
            node.children = new List<JsonTreeItem>();
            node.attributes = new JsonTreeItemCustomAttribute();
            if (index <= 2)
            {
                node.hasCheckbox = "false";
                node.attributes.color = "#0000ff";
                node.attributes.image = "../../Content/Image/个人.png";
            }

            node.value = "11";
            node.hasLogic = hasLogic;
            if (!string.IsNullOrEmpty(icon))
                node.iconCls = icon;
            
            return node;
        }

        public ActionResult GetTreeGrid()
        {
            JsonResult json = new JsonResult();
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            IList<JsonTreeItem> items = new List<JsonTreeItem>();
            //作业组树节点
            JsonTreeItem root = GetNode(1);
            root.children.Add(GetNode(2,true));
            root.children.Add(GetNode(3, false, "icon-help"));
            root.children[0].children.Add(GetNode(4,true));
            root.children[0].children.Add(GetNode(5));
            root.children[0].children.Add(GetNode(6));
            items.Add(root);
            json.Data = items;
            return json;
        }
    }
}
